<?
$directory = dirname(__FILE__);
require_once($directory . "/functions.php");
extract($_GET);
	if($_POST) {
		extract($_POST);
		if(strpos($save_location, 'drizzo-files') == 0) {
			$save_location = "drizzo-files" . $save_location;
		}
		$upload_dir = str_replace('drizzo/engine','/',$directory) . $save_location . '/';
		if($new_folder != '') {
			if(get_magic_quotes_gpc()) {
				$new_folder = stripslashes($new_folder);
			}
			$new_folder = trim(strtolower(preg_replace('/([\W])/','-',$new_folder)));
			$new_dir = @drizzo_mkdir($upload_dir . $new_folder);
			if($new_dir) {
				$upload_dir = $upload_dir . $new_folder . '/';
			} else {
				$response = "<p>The new folder could not be created.</p>";
				$upload_dir = null;
			}
		}
		if(is_writable($upload_dir)) {
			if(!empty($_FILES['file']) && $_FILES['file']['error'] == 0) {
				$filename = basename($_FILES['file']['name']);
				if(file_exists($upload_dir . $filename) && $file_overwrite == 'no') {
					$response = "<p>A file with that name already exists.</p>";
				} elseif(file_exists($upload_dir . $filename) && $file_overwrite == 'yes') {
					unlink($upload_dir . $filename);
					$status = move_uploaded_file($_FILES['file']['tmp_name'],$upload_dir . $filename);
				} else {
					$status = move_uploaded_file($_FILES['file']['tmp_name'],$upload_dir . $filename);
				}
				if($status == true) {
					$response = "<p>The file was uploaded.</p>";
				} else {
					$response = "<p>There was an error uploading the file.</p>";
				}
			} else {
				$response = "<p>There was an error uploading the file.</p>";
			}
		} else {
			$response = "<p>The specified directory ($save_location) is not writable by the server</p>";
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
	<head>
		<link rel="stylesheet" href="<? echo drizzo_admin_css(); ?>" type="text/css" media="all" title="main" charset="utf-8" />
	</head>
	<body id="file_uploader">
		<? echo $response; ?>
		<p><strong>Upload a file</strong></p>
		<form method="post" action="" name="upload_form" enctype="multipart/form-data">
			<label for="save_location">Save to</label> <? echo drizzo_upload_directories_dropdown($parent_dir); ?><br class="clear" />
			<label for="new_directory">New Folder<a href="javascript:void(0)" title="Help">?<span class="explain">Enter a name for a new folder which will be created under the folder selected above. IE, if you enter 'folder-a' here and 'gallery' is selected above, the new folder will be at 'gallery/folder-a'.</span></a></label> <input type="text" name="new_folder" /><br class="clear" />
			<label for="file_overwrite">Remove Old File<a href="javascript:void(0)" title="Help">?<span class="explain">If a file is found in the same location with the same name this option will delete it and replace it with the new one.  If this option is not checked and a matching file is found, you will get an error response.</span></a></label> <select name="file_overwrite"><option value="no" select>No</option><option value="yes">Yes</option></select><br class="clear" />
			<input type="hidden" name="MAX_FILE_SIZE" value="83886080" />
			<label for="file">File</label> <input type="file" name="file" /><br class="clear" />
			<input type="submit" class="submit" name="upload_submit" value="Upload" />
		</form>
	</body>
</html>
